﻿using System;
using System.Windows.Forms;
using FBProject.BusinessLogic;
using FBProject.BusinessLogic.MSProjectProxies;

namespace FBProject
{
    public partial class ActiveProjectInfo : UserControl
    {
        public event EventHandler ActiveProjectChanged;

        public bool ActiveProjectOk { get; private set; }

        public ActiveProjectInfo()
        {
            InitializeComponent();

            NoActiveMSProjectGroupBox.Location = ActiveMSProjectInfoGroupBox.Location;
            NoActiveMSProjectGroupBox.Size = ActiveMSProjectInfoGroupBox.Size;

            if (!this.DesignMode)
            {
                RefreshActiveProjectInfo();
                UpdateActiveProjectInfoTimer.Start();
            }
        }

        private void OnActiveProjectChanged()
        {
            if (ActiveProjectChanged != null)
            {
                ActiveProjectChanged(this, EventArgs.Empty);
            }
        }

        private void UpdateActiveProjectInfoTimer_Tick(object sender, EventArgs e)
        {
            RefreshActiveProjectInfo();
        }

        private void CreateNewMSProjectFileButton_Click(object sender, EventArgs e)
        {
            var originalWindowState = this.ParentForm.WindowState;
            this.ParentForm.WindowState = FormWindowState.Minimized;
            MSProjectApplicationProxy.CreateNewMSProject();
            this.ParentForm.WindowState = originalWindowState;
            this.ParentForm.BringToFront();
        }

        private void OpenExistingMSProjectButton_Click(object sender, EventArgs e)
        {
            var originalWindowState = this.ParentForm.WindowState;
            this.ParentForm.WindowState = FormWindowState.Minimized;
            MSProjectApplicationProxy.OpenMSProject();
            this.ParentForm.WindowState = originalWindowState;
            this.ParentForm.BringToFront();
        }

        private void OpenLastMSProjectButton_Click(object sender, EventArgs e)
        {
            var originalWindowState = this.ParentForm.WindowState;
            this.ParentForm.WindowState = FormWindowState.Minimized;
            MSProjectApplicationProxy.OpenMSProject(ApplicationConfiguration.LastMSProject);
            this.ParentForm.WindowState = originalWindowState;
            this.ParentForm.BringToFront();
        }

        private void RefreshActiveProjectInfo()
        {
            var previousProjectName = ProjectNameLabel.Text;

            var activeProjectOk = false;
            try
            {
                var activeMSProject = MSProjectApplicationProxy.GetActiveProject();
                ProjectNameLabel.Text = activeMSProject.FullName;
                if (activeMSProject.CreationDate is DateTime)
                {
                    var creationDate = (DateTime)activeMSProject.CreationDate;
                    CreatedLabel.Text = ApplicationHelper.GetFormattedDateTimeDifference(creationDate.ToUniversalTime());
                }
                else
                {
                    CreatedLabel.Text = string.Empty;
                }
                if (activeMSProject.LastSaveDate is DateTime)
                {
                    var lastSaveDate = (DateTime)activeMSProject.LastSaveDate;
                    LastUpdateLabel.Text = ApplicationHelper.GetFormattedDateTimeDifference(lastSaveDate.ToUniversalTime());
                }
                else
                {
                    LastUpdateLabel.Text = string.Empty;
                }
                activeProjectOk = true;
            }
            catch (NoActiveMSProjectException)
            {
                ProjectNameLabel.Text = string.Empty;
                CreatedLabel.Text = string.Empty;
                LastUpdateLabel.Text = string.Empty;
            }

            OpenLastMSProjectButton.Enabled =
                !string.IsNullOrEmpty(ApplicationConfiguration.LastMSProject)
                && (ApplicationConfiguration.LastMSProject != ProjectNameLabel.Text);

            this.ActiveProjectOk = activeProjectOk;

            if (activeProjectOk)
            {
                NoActiveMSProjectGroupBox.Visible = false;
                ActiveMSProjectInfoGroupBox.Visible = true;
                if (previousProjectName != ProjectNameLabel.Text)
                {
                    OnActiveProjectChanged();
                }
            }
            else
            {
                ActiveMSProjectInfoGroupBox.Visible = false;
                NoActiveMSProjectGroupBox.Visible = true;
                if (previousProjectName != string.Empty)
                {
                    OnActiveProjectChanged();
                }
            }
        }
    }
}
